package com.damage.materiel_dept.oa.completeOrder;

import com.damage.common.entity.Order;
import com.damage.common.entity.PurchaseInvoice;
import com.damage.common.oa.completeOrder.CompleteOrderConfig;
import com.damage.common.service.IPublicService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.User;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RemarkInspectResultService {

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IPublicService publicService;

    @Autowired
    private IdentityService identityService;

    public void remrkInspectResult(String taskId,String comment){
        //关联实体类
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        String processInstanceId = task.getProcessInstanceId();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();
        String businessKey = processInstance.getBusinessKey();
        PurchaseInvoice purchaseInvoice = publicService.findPurchaseInvoiceById(Integer.parseInt(businessKey));
        //设置供应商变量
        Order order = publicService.findOrderById(purchaseInvoice.getOrderId());
        User user = identityService.createUserQuery()
                .userFirstName(order.getOrderReceiver())
                .singleResult();
        taskService.setVariable(taskId, CompleteOrderConfig.SUPPLIER,user.getId());
        //添加评论
        taskService.addComment(taskId,processInstanceId,comment);
        //完成任务
        taskService.complete(taskId);
    }

}
